$(function(){
    $('#listGrid').treegrid({
        toolbar: '#toolbar',
        title: '模块（菜单）列表',
        iconCls: 'icon icon-chart_organisation',
        fit: true,
        fitColumns:true,
        nowrap: false,
        rownumbers: true,
        resizable: true,
        collapsible: false,
        url: '/sys/menu/list',
        queryParams:{
            _token : $('meta[name="csrf-token"]').attr('content'),
            curr_menu_id : $('meta[name="curr_menu_id"]').attr('content')
        },
        idField: 'id',
        treeField: 'name',
        frozenColumns: [[
            { title: '模块（菜单）名称', field: 'name', width: 250 },
        ]],
        columns: [[
            { title: 'id', field: 'id', hidden: true },
            { title: 'menu_level', field: 'menu_level', hidden: true },
            { title: '菜单图标', field: 'icon_style', width: 130,align:'center'},
            { title: '菜单地址', field: 'menu_url', width: 200,align:'center' },
            { title: '是否有效', field: 'is_active', width: 100,formatter:format_active,align:'center'},
            { title: '排序', field: 'sort', width: 100 ,align:'center',sortable:true},
            { title: '权限编码', field: 'code', width: 100 ,align:'center'},
            { title: '访问权限', field: 'permission', width: 100 ,align:'center'},
            { title: '创建时间', field: 'created_at', width: 180,align:'center' ,sortable:true},
            { title: '更新时间', field: 'updated_at', width: 180 ,align:'center',sortable:true},
            { title: '菜单操作', field: '11' ,align:'center',width: 250,formatter:
                function(value,row,index){
                    return "<a href='#' row_id ='"+row.id+"' class='easyui-linkbutton btn_set'>按钮设置</a>|<a class='easyui-linkbutton btn_data' row_id ='"+row.id+"'  href='#'>数据设置</a>"
                }
            }

        ]]
    });


})

function openAdd(){
    var rows = $('#listGrid').treegrid('getSelected');
    if (null == rows) {
        parent_id = 0;
        parent_name = '';
    }else{
        parent_id = rows.id;
        parent_name = rows.name;
    }

    $('#form_dialog').dialog({
        title: '添加菜单',
        width: 600,
        height: 500,
        closed: false,
        cache: false,
        href: '/sys/menu/add?parent_id='+parent_id+'&parent_name='+parent_name,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function openEdit(){
    var rows = $('#listGrid').datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行" , "info");
        return;
    }
    $('#form_dialog').dialog({
        title: '编辑菜单',
        width: 600,
        height: 500,
        closed: false,
        cache: false,
        href: '/sys/menu/edit?menu_id='+rows.id,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function save(){
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:'/sys/menu/save',
        type:'post',
        data:$('#info-form').serialize(),
        dataType:'json',
        success:function (data) {
            if(data.code == 200){
                $('#listGrid').treegrid("reload");
                $('#form_dialog').dialog('close');
            }else{
                $.messager.alert("提示信息", data.data , "error");
            }
        }
    });
}

function reload(){
    $('#listGrid').datagrid("reload");
}
function  openIcon() {
    $('#icon_dialog').dialog({
        title: '选择图标',
        width: 400,
        height: 200,
        closed: false,
        cache: true,
        href: '/css/iconlist.html?v=' + Math.random() ,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: function () {
                $('#icon_style').textbox({value:$.trim($('.l-btn-icon-left').attr('data-selected-class'))});
                $('#icon_dialog').dialog('close');
            }
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#icon_dialog').dialog('close');
            }
        }]
    });
}